<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:ui="http://java.sun.com/jsf/facelets"
      template="/resources/template/template.xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:p="http://primefaces.org/ui"
      >



    <h:body>

        <ui:composition template="/resources/template/template.xhtml">

            <ui:define name="content">

                <h:panelGroup rendered="#{!webUserController.hasPrivilege('AdminManagingUsers')}" >
                    <h:outputLabel value="You are NOT authorized"/>
                </h:panelGroup>

                <h:panelGroup   rendered="#{webUserController.hasPrivilege('AdminManagingUsers')}" >
                    <h:form  >
                        <p:panel header="Allow user to log on to Departments" >

                            <f:facet name="header" >
                                <h:outputLabel value="Departments User Can Log"/>
                                <p:commandButton ajax="false" value="Back" action="#{webUserController.backToViewUsers()}" style="float: right;" >
                                </p:commandButton>
                            </f:facet>
                            <h:panelGrid  columns="2" >                          
                                <h:outputLabel value="User"/>
                                <p:autoComplete id="user" forceSelection="true" converter="webUs"  
                                                completeMethod="#{webUserController.completeUser}" var="us" 
                                                itemLabel="#{us.webUserPerson.name}" itemValue="#{us}" 
                                                value="#{userDepartmentController.selectedUser}" >
                                    <f:ajax event="itemSelect" execute="user" render="ins" />
                                </p:autoComplete>
                            </h:panelGrid>

                            <h:panelGrid columns="2" styleClass="alignTop" >
                                <h:panelGrid columns="2" >
                                    <h:outputText value="Institution" ></h:outputText>
                                    <p:selectOneMenu  value="#{userDepartmentController.currentInstituion}" id="ins2"  >
                                        <f:selectItem itemLabel="Please select an institutie"/>
                                        <f:selectItems value="#{institutionController.items}" var="i" itemLabel="#{i.name}" itemValue="#{i}" />
                                        <p:ajax update="dept2" process="ins2" event="change"/>
                                    </p:selectOneMenu>
                                    <h:outputText value="Department" ></h:outputText>
                                    <p:selectOneMenu id="dept2"  value="#{userDepartmentController.currentDepartment}">
                                        <f:selectItem itemLabel="Please select a Department"/>
                                        <f:selectItems value="#{userDepartmentController.currentInsDepartments}" var="d" itemLabel="#{d.name}" itemValue="#{d}" />
                                    </p:selectOneMenu>
                                    <p:commandButton id="btnAddDepartments" ajax="false" value="Add" action="#{userDepartmentController.addDepartmentForUser()}"/>
                                    <p:defaultCommand target="btnAddDepartments"/>
                                </h:panelGrid>

                                <h:panelGrid columns="2" id="ins">
                                    <p:panel header="Departments allowed for this user">
                                        <p:dataTable  value="#{userDepartmentController.items}" var="i">
                                            <p:column headerText="Institution">
                                                <h:outputLabel value="#{i.department.institution.name}"/>                              
                                            </p:column>
                                            <p:column headerText="Department">
                                                <h:outputLabel value="#{i.department.name}"/>                              
                                            </p:column>
                                            <p:column >
                                                <p:commandButton ajax="false" value="Remove" action="#{userDepartmentController.delete()}" onclick="onSubmitButton();">
                                                    <f:setPropertyActionListener value="#{i}" target="#{userDepartmentController.current}"/>
                                                </p:commandButton>
                                            </p:column>
                                        </p:dataTable>                        
                                    </p:panel>

                                </h:panelGrid>

                            </h:panelGrid>








                        </p:panel>

                    </h:form>

                </h:panelGroup>

            </ui:define>

        </ui:composition>
    </h:body>

</html>
